package stud.mpgame.flash.scene;

import stud.mpgame.flash.IScene;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.display.Sprite;
import flash.display.DisplayObject;
import stud.mpgame.network.NetworkMonitor;
import stud.mpgame.flash.preview.MapInfoMessageDescription;
import stud.mpgame.flash.preview.ReadyToGameMessageDescription;
import stud.mpgame.flash.preview.MapInfo;
import stud.mpgame.flash.preview.ReadyToGame;
import stud.mpgame.network.MessageDescription;
import stud.mpgame.flash.map.JsonDataProvider;
import stud.mpgame.flash.map.GameMap;
import stud.mpgame.network.Message;

/**
 * ...
 * @author ghfjsdkl
 */

class LoadingScene extends Sprite,implements IScene
{
	public static inline var NAME = "LoadingScene";
    private var loadingField:TextField;
    private var networkMonitor:NetworkMonitor;
    private var jsonData: JsonDataProvider;
    
	public function new() {
        super();
        
		loadingField = new TextField();
		loadingField.border = false;
		loadingField.type = TextFieldType.DYNAMIC;
		loadingField.width = 200;
		loadingField.height = 18;
		loadingField.text = "Loading...";
		loadingField.x = 10;
		loadingField.y = 10;
		
		addChild(loadingField);
	}
	
	public function getName():String {
		return NAME;
	}
	
	public function init(data:Dynamic):Void {
        this.networkMonitor = data.networkMonitor;
        MessageDescription.registerDescription(MapInfoMessageDescription.getInstance());
        MessageDescription.registerDescription(ReadyToGameMessageDescription.getInstance());
        
        networkMonitor.getMessageDispatcher().addMessageReceivedListener(MapInfoMessageDescription.getInstance(), mapInfoReceived);
	}

	public function mapInfoReceived(message:Message) {
		
        trace("Map info received");
		var mapInfo:MapInfo = cast(message.getUserObject(MapInfo), MapInfo);
       	var mapURL:String = mapInfo.mapURL;
                
        jsonData = new JsonDataProvider();
                
		//jsonData.provide(mapURL,mapFunction);
        
        var readyMessage:Message = new Message(networkMonitor, ReadyToGameMessageDescription.getInstance());//временный вариант, пока не будет реализована загрузка карты
        var ready:ReadyToGame = new ReadyToGame("testuser", 1);
        readyMessage.putUserObject(ready);
        readyMessage.send();
        Main.getInstance().setScene(GameScene.NAME, {networkMonitor: networkMonitor});
        trace("Ready sent");
	}
    
    public function mapFunction(map:GameMap):Void {
        //map.DrawMap();
        var readyMessage:Message = new Message(networkMonitor, ReadyToGameMessageDescription.getInstance());
        var ready:ReadyToGame = new ReadyToGame("testuser", 1);
        readyMessage.putUserObject(ready);
        readyMessage.send();
        trace("Ready sent");
        Main.getInstance().setScene(GameScene.NAME, {networkMonitor: networkMonitor});
    }
    
	public function clear():Void {
	}
	
	public function update(deltaTime:Int):Void {
	}
	
	public function getContentRoot():DisplayObject {
		return this;
	}
}
